<?php

App::uses('AppController', 'Controller');

/**
 * Hojasrutas Controller
 *
 * @property Hojasruta $Hojasruta
 * @property PaginatorComponent $Paginator
 */
class HojasrutasController extends AppController {

    /**
     * Components
     *
     * @var array
     */
    public $components = array('Paginator');

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->Hojasruta->recursive = 0;
        $this->set('hojasrutas', $this->Paginator->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->Hojasruta->exists($id)) {
            throw new NotFoundException(__('Invalid hojasruta'));
        }
        $options = array('conditions' => array('Hojasruta.' . $this->Hojasruta->primaryKey => $id));
        $this->set('hojasruta', $this->Hojasruta->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->Hojasruta->create();
            if ($this->Hojasruta->save($this->request->data)) {
                $this->Session->setFlash(__('The hojasruta has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The hojasruta could not be saved. Please, try again.'));
            }
        }
        $documentos = $this->Hojasruta->Documento->find('list');
        $users = $this->Hojasruta->User->find('list');
        $destinos = $this->Hojasruta->Destino->find('list');
        $this->set(compact('documentos', 'users', 'destinos'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        if (!$this->Hojasruta->exists($id)) {
            throw new NotFoundException(__('Invalid hojasruta'));
        }
        if ($this->request->is(array('post', 'put'))) {
            if ($this->Hojasruta->save($this->request->data)) {
                $this->Session->setFlash(__('The hojasruta has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The hojasruta could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('Hojasruta.' . $this->Hojasruta->primaryKey => $id));
            $this->request->data = $this->Hojasruta->find('first', $options);
        }
        $documentos = $this->Hojasruta->Documento->find('list');
        $users = $this->Hojasruta->User->find('list');
        $destinos = $this->Hojasruta->Destino->find('list');
        $this->set(compact('documentos', 'users', 'destinos'));
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->Hojasruta->id = $id;
        if (!$this->Hojasruta->exists()) {
            throw new NotFoundException(__('Invalid hojasruta'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Hojasruta->delete()) {
            $this->Session->setFlash(__('The hojasruta has been deleted.'));
        } else {
            $this->Session->setFlash(__('The hojasruta could not be deleted. Please, try again.'));
        }
        return $this->redirect(array('action' => 'index'));
    }

    public function ajaxpendientes() {
        //$this->render = false;
        //echo 'llego';
        $this->layout = 'ajax';
        $idUsuario = $this->Session->read('Auth.User.id');
        $cuentaRecienLlegados = $this->Hojasruta->find('count', array(
            'recursive' => -1,
            'conditions' => array(
                'Hojasruta.destino_id' => $idUsuario,
                'Hojasruta.estado' => 'Entregado'
            )
        ));

        $this->set(compact('recienLlegados', 'cuentaRecienLlegados'));
    }

   

}
